package p1.ia;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;


public class PanelMapa extends JPanel implements Runnable{
   
  public double x, y, alto = 100, ancho = 100;
  public Color c = Color.red;
  private int tm = 1000;
  public Thread t1;

  public void threadstr(){
    t1 = new Thread(this);
    t1.start();
  }

  public void run(){
    try{
      while(true){
	Thread.sleep(tm);
	cambiarColor();
	cambiarXY();
	repaint();
      }
    }catch (InterruptedException e){
      System.out.println("Error en el hilo");
    }
  }

  public PanelMapa(){
    super();
    repaint();
  }
 
  public void cambiarXY(){
	x = (int)(Math.random() * 400);
	y = (int)(Math.random() * 600);
  }

  public void cambiarColor(){
    c = new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256));
    repaint();
  } 
  
  public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setColor(c);
    Rectangle2D.Double r = new Rectangle2D.Double (x,y, ancho, alto);
    g2.fill(r); //draw.
  }
}